import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:rfid/event/id_event.dart';

import '../main.dart';

// typedef Cpara = Int32 Function(Int32);
// typedef DartPara = int Function(int);

// typedef Cpara2 = Int32 Function();
// typedef DartPara2 = int Function();

// typedef Cpara3 = Int32 Function(Int32);
// typedef DartPara3 = int Function(int);

// typedef Cpara5 = Pointer<Utf8> Function();
// typedef DartPara5 = Pointer<Utf8> Function();
//
// typedef Cpara4 = Pointer<Utf8> Function();
// typedef DartPara4 = Pointer<Utf8> Function();

// typedef Cparasam = Int32 Function(Pointer<Utf8>);
// typedef DartParasam = int Function(Pointer<Utf8>);

// typedef Cparaauth = Int32 Function();
// typedef DartParaauth = int Function();

class IdHelper{
  static IdHelper get instance => _getInstance();

  static IdHelper? _idHelper;

  static IdHelper _getInstance() {
    _idHelper ??= IdHelper();
    return _idHelper!;
  }

  DynamicLibrary lib = DynamicLibrary.open('termb64.dll');


  close(){
    var close = lib.lookupFunction<Int32 Function(), int Function()>("CloseComm");
    int re2 = close();

    print('close--$re2');
  }

  open(){
    var close = lib.lookupFunction<Int32 Function(), int Function()>("CloseComm");
    int re2 = close();

    print('close--$re2');

    var init = lib.lookupFunction<Int32 Function(Int32), int Function(int)>("InitComm");
    int re = init(2401);
    print('init--$re');
    if(re == 0) {
      // var GetSAMID= lib.lookupFunction<Cparasam, DartParasam>("GetSAMID");
      // // final p = calloc<Utf8>();
      // final utf8String = ''.toNativeUtf8();
      // int rr = GetSAMID(utf8String);
      // malloc.free(utf8String);
      // // calloc.free(p);
      // print('GetSAMID--$rr');
      //
      // // lib.close();
      // if(rr == 0){

      _startRead();
    }
  }

  _startRead(){

    try{

      var auth = lib.lookupFunction<Int32 Function(), int Function()>("Authenticate");
      int ra = auth();
      print('auth-- $ra');
      if(ra == 0){
        var read = lib.lookupFunction<Int32 Function(Int32), int Function(int)>("Read_Content");
        int re3 = read(1);
        print('read-- $re3');
        if(re3 == 0){
          get();
          // var getName = lib.lookupFunction<Cpara4, DartPara4>("GetName");
          // Pointer<Utf8> ptrName = getName();
          // print('ptrName = ${ptrName.toDartString()}');
          // malloc.free(ptrName);
          //
          // var getIDNo = lib.lookupFunction<Cpara5, DartPara5>("GetIDNo");
          // Pointer<Utf8> ptr = getIDNo();
          // print('ptr = ${ptr.toDartString()}');
          // eventFactory.fire(IdEvent(ptr.cast<Utf8>().toDartString()));
          // malloc.free(ptr);
        }
      }else{
        Future.delayed(const Duration(seconds: 1),(){
          _startRead();
        });
      }
    }catch(e){
      print(e);
    }
  }

  get(){
    var getIDNo = lib.lookupFunction<Pointer<Utf8> Function(), Pointer<Utf8> Function()>("GetIDNo");
    Pointer<Utf8> ptr = getIDNo();
    print('ptr = ${ptr.toDartString()}');
    // eventFactory.fire(IdEvent(ptr.cast<Utf8>().toDartString()));
    malloc.free(ptr);

    // var getName = lib.lookupFunction<Cpara4, DartPara4>("GetName");
    // Pointer<Utf8> ptrName = getName();
    // print('ptrName = ${ptrName.toDartString()}');
    // malloc.free(ptrName);


  }

  /// utf16转unicode
  static int utf162unicode(List<int> utf8s) {
    if (utf8s.length == 1) {
      return utf8s[0];
    } else if (utf8s.length == 2) {
      int vh = (utf8s[0] - 0xD800) << 10;
      int vl = utf8s[1] - 0xDC00;
      return vh + vl + 0x10000;
    }
    return 0;
  }

  String unicodeToStr(String unicode) {
    var codePoints = unicode.split('\\\\u').skip(1).map((str) => int.parse(str, radix: 16));
    return String.fromCharCodes(codePoints.toList());
  }
}